package com.qding.park.common.utils.rchl;



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.util.Date;

public class RchlFileUtil {

	
	public static void newFileForAppend(String content,String filePath,boolean appendFlag){
		
		FileWriter writer =  null;
		try {
			if(appendFlag){
				writer = new FileWriter(filePath,true);
			}else{
				writer = new FileWriter(filePath);
			}
			
			writer.write(content);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if( writer != null ){
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	

	public static String filePathAddDate(String filePath){
		String path = filePath +"/"+ RchlDateUtil.getDateyyyymm(new Date())+"/"+RchlDateUtil.getDateyyyymmdd(new Date());
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		return path;
	}
	
	public static final InputStream byteToInputStream(byte[] buf) {  
        return new ByteArrayInputStream(buf);  
    }  
  
    public static final byte[] inputStreamTobyte(InputStream inStream)  
            throws IOException {  
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
        byte[] buff = new byte[100];  
        int rc = 0;  
        while ((rc = inStream.read(buff, 0, 100)) > 0) {  
            swapStream.write(buff, 0, rc);  
        }  
        byte[] in2b = swapStream.toByteArray();  
        return in2b;  
    }  
    
    public static void delFile(String path) {
		File f = new File(path);
		if (f.exists()) {
			f.delete();
		}

	}
    
    public static void copyFile(File sourct, File target) {

        FileInputStream fi = null;
        FileOutputStream fo = null;
        FileChannel in = null;
        FileChannel out = null;

        try {
            fi = new FileInputStream(sourct);
            fo = new FileOutputStream(target);
            in = fi.getChannel();//得到对应的文件通道
            out = fo.getChannel();//得到对应的文件通道
            in.transferTo(0, in.size(), out);//连接两个通道，并且从in通道读取，然后写入out通道
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                fi.close();
                in.close();
                fo.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}
